package com.wjb.service.impl;

import com.wjb.mapper.ProductCategoryMapper;
import com.wjb.model.EbChildProductCategory;
import com.wjb.model.EbChildProductCategory;
import com.wjb.model.EbProductCategory;
import com.wjb.service.ProductCategoryService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ProductCategoryImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<EbChildProductCategory> listByPage(Integer page, Integer pageSize) {
         int count=0;
        List<EbChildProductCategory> eCPC = productCategoryMapper.findByPage();
        for (int i=0;i<eCPC.size();i++)
        {
            count++;
            for (int j=0;j<eCPC.get(i).getChildEbProductCategory().size();j++)
            {
                count++;
                eCPC.get(i).getChildEbProductCategory().get(j).setParent(eCPC.get(i));
            }
        }

        List<EbChildProductCategory> eCPC2 = new ArrayList<>();
        for (int i=0;i<eCPC.size();i++)
        {
            eCPC2.add(eCPC.get(i));

            for (int j=0;j<eCPC.get(i).getChildEbProductCategory().size();j++)
            {
                eCPC2.add(eCPC.get(i).getChildEbProductCategory().get(j));

                eCPC.get(i).getChildEbProductCategory().get(j).setParent(eCPC.get(i));
            }
        }

        List<EbChildProductCategory> eCPC3 = new ArrayList<>();
        Integer from = (page-1)*pageSize;
        if (from+pageSize<=count)
        {
            for (int i=from;i<pageSize+from;i++)
                eCPC3.add(eCPC2.get(i));
        }
        else
        {
            if (from>=count)
                return null;
            else
            {
                for (int i=from;i<count;i++)
                {
                    eCPC3.add(eCPC2.get(i));
                }
            }
        }
        System.out.println(eCPC3);
        List<EbChildProductCategory> eCPC4 = new ArrayList<>();
        for (int i=0;i<eCPC3.size();i++)
        {
            if (eCPC3.get(i).getParent()==null)
                eCPC4.add(eCPC3.get(i));
            if (eCPC3.get(i).getParent()!=null)
            {
                if (!eCPC4.contains(eCPC3.get(i).getParent()))
                {
                    eCPC4.add(eCPC3.get(i).getParent());
                    eCPC4.get(eCPC4.size()-1).setStatus(1);
                }
            }
        }
        System.out.println(eCPC4);
        for (int i=0;i<eCPC4.size();i++)
        {
            List<EbChildProductCategory> eCPC5 = new ArrayList<>();
            for (int j=0;j<eCPC3.size();j++)
            {
                if (eCPC4.get(i).equals(eCPC3.get(j).getParent()))
                {
                    System.out.println("我是大魔王");
                    eCPC5.add(eCPC3.get(j));
                }
            }
            eCPC4.get(i).setChildEbProductCategory(eCPC5);
        }
        return eCPC4 ;
    }

    @Override
    public Integer getMaxPage(Integer pageSize) {
        List<EbChildProductCategory> eCPC = productCategoryMapper.findByPage();

                   int count = 0;
        for (int i=0;i<eCPC.size();i++)
        {
            count++;
            for (int j=0;j<eCPC.get(i).getChildEbProductCategory().size();j++)
            {
                count++;
            }
        }
        int maxPage = count/pageSize;
        if (count%pageSize!=0)
            maxPage++;
        return maxPage;
    }

    @Override
    public List<EbChildProductCategory> oneParent() {
        return productCategoryMapper.oneParent();
    }

    @Override
    public List<EbChildProductCategory> twoChild() {
        return productCategoryMapper.twoChild();
    }

    @Override
    public Integer saveParent(EbChildProductCategory ebChildProductCategory) {
             productCategoryMapper.saveParent(ebChildProductCategory);
                ebChildProductCategory.setEpcParentId(ebChildProductCategory.getEpcId());
        return productCategoryMapper.updateByPrimaryKey(ebChildProductCategory);
    }

    @Override
    public Integer saveChild(EbChildProductCategory eCPC) {
        return  productCategoryMapper.saveChild(eCPC);
    }

    @Override
    public EbChildProductCategory selectOne(Integer id) {
        return productCategoryMapper.selectByOne(id);
    }

    @Override
    public Integer updateECPC(EbChildProductCategory eCPC) {
        return productCategoryMapper.updateByPrimaryKey(eCPC);
    }

    @Override
    public Integer delectECPC(Integer epcId) {
        EbChildProductCategory eCPC = productCategoryMapper.selectByOne(epcId);
        if (eCPC.getEpcId().equals(eCPC.getEpcParentId()))
            return productCategoryMapper.deleteParent(epcId,epcId);

        return productCategoryMapper.deleteChild(epcId);
    }
}
